const service = require('../../service/mycard.js')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
//import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: false,
    customerId: wx.getStorageSync('userInfo').id,
    businesscardList: '',
    getCount: 0,
    cardId: 0,
    cardList: [],
    showdefault: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: "加载中...",
    });
    service.getDefaultCard(this.data.customerId, res => {
      //console.log(res)

      this.setData({
        businesscardList: res.data,
        cardId: res.data.cardId
      })
      service.getCount(this.data.cardId, rest => {
        console.log(rest)
        wx.hideLoading();
        this.setData({
          getCount: rest.data
        })
      })
    })
    service.cardList(this.data.customerId, res => {
      //  console.log(res)
      this.setData({
        cardList: res.data
      })
    })
  },
  listItemTo(e) {
    // console.log(e)
    let self = this;
    self.setData({
      active: !self.data.active
    })
  },
  goadd() {
    wx.navigateTo({
      url: "/pages/mycard/edit/index"
    });
  },
  /**
   * 设为默认名片
   */
  settingTo() {
    service.settingCard({
      "customerId": this.data.customerId,
      "cardId": this.data.cardId
    }, res => {
      if (res.code == "00000") {
        wx.showToast({
          title: '设置成功',
          icon: 'success',
          duration: 2000
        })
        this.onLoad()
      } else {
        wx.showToast({
          title: '设置失败',
          icon: 'none',
          duration: 2000
        })
      }
    })

  },
  /**
   * 删除名片
   */
  delecardTo(e) {
    // console.log(e)
    let cardid = e.currentTarget.dataset.index.cardId
    Dialog.confirm({
        title: '提示',
        message: '是否删除该名片？',
      })
      .then(() => {
        service.deleCard(cardid, res => {
          if (res.data == 0) {

            wx.showToast({
              title: '默认名片不能删除!',
              icon: 'none',
              duration: 2000
            })

          } else if (res.data == 1) {
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 2000
            })
            this.onLoad()
          }

        })
      })
      .catch(() => {
        // on cancel
      });

  },
  resetTo(e) {
    // console.log(e)
    
    this.setData({
      businesscardList: e.currentTarget.dataset.index,
      cardId: e.currentTarget.dataset.index.cardId,

      showdefault: true
    })
    service.getCount(this.data.cardId, rest => {
      console.log(rest)
      wx.hideLoading();
      this.setData({
        getCount: rest.data
      })
    })
  },
  updateeditTo(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/mycard/updateCard/index?cardId=' + e.currentTarget.dataset.index,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
     console.log(res)
    if (res.from === "button") {
      // 通过按钮触发
      var data = res.target.dataset;
      return {
        title: data.title + '的名片',
        imageUrl: "https://hopermall.oss-cn-beijing.aliyuncs.com/renrenbo/card.png",
        path: "/pages/mycard/share/index?id=" + data.cardid + "&custId=" + wx.getStorageSync('userInfo').id,
        success: function (res) {
          // 转发成功
          console.log("转发成功");
        },
        fail: function (res) {
          // 转发失败
          console.log("转发失败");
        },
      };
    }
  }
})